package com.power.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.power.domain.SysUser;
import com.power.model.Result;
import com.power.service.SysUserService;
import com.power.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 系统管理员控制层
 * @Author dingchunlin
 * @Date 2024/07/12 17:52
 * @Version 1.0.0
 */
@Api(tags = "系统管理员接口管理")
@RequestMapping("/sys/user")
@RestController
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;
    /**
     * 查询登录的用户信息
     * @return 用户信息
     */
    @ApiOperation(value = "查询登录的用户信息")
    @GetMapping("/info")
    public Result<SysUser> loadSysUserInfo(){
        // 获取用户标识
        Long loginUserId = AuthUtils.getUserId();
        // 根据用户标识查询用户信息
        SysUser sysUser = sysUserService.getById(loginUserId);
        // 返回用户信息
        return Result.success(sysUser);
    }

    /**
     * 分页查询系统管理员
     * @param current 页码
     * @param size 每页数量
     * @param username 用户名
     * @return 分页查询结果
     */
    @ApiOperation(value = "多条件分页查询系统管理员")
    @GetMapping("/page")
    @PreAuthorize("hasAnyAuthority('sys:user:page')")
    public Result<Page<SysUser>> page(@RequestParam Long current,
                                      @RequestParam Long size,
                                      @RequestParam(required = false) String username){
        Page<SysUser> sysUserPage = new Page<>(current, size);
        LambdaQueryWrapper<SysUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.hasText(username), SysUser::getUsername, username);
        lambdaQueryWrapper.orderByDesc(SysUser::getCreateTime);
        // 调用service层分页查询方法

        Page<SysUser> userPage = sysUserService.page(sysUserPage, lambdaQueryWrapper);

        return Result.success(userPage);
    }

    /**
     * 新增系统管理员
     * @param sysUser 系统管理员信息
     * @return 新增结果
     */
    @ApiOperation(value = "新增系统管理员")
    @PostMapping
    @PreAuthorize("hasAnyAuthority('sys:user:save')")
    public Result saveSysUser(@RequestBody SysUser sysUser){
        Integer result = sysUserService.saveSysUser(sysUser);
        return Result.handle(result > 0);
    }

    /**
     * 根据标识查询管理员信息
     * @param id 管理员标识
     * @return 管理员信息
     */
    @ApiOperation(value = "根据标识查询管理员信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAnyAuthority('sys:user:info')")
    public Result<SysUser> loadSysUserInfo(@PathVariable Long id){
        SysUser sysUser = sysUserService.querySysUserByUserId(id);
        return Result.success(sysUser);
    }

    /**
     * 修改系统管理员
     * @param sysUser 系统管理员信息
     * @return 修改结果
     */
    @ApiOperation(value = "修改系统管理员")
    @PutMapping
    @PreAuthorize("hasAnyAuthority('sys:user:update')")
    public Result modifySysUserInfo(@RequestBody SysUser sysUser){
        Integer result = sysUserService.modifySysUserInfo(sysUser);
        return Result.handle(result > 0);
    }

    /**
     * 批量或单个删除系统管理员
     * @param userIds 管理员标识列表
     * @return 删除结果
     */
    @ApiOperation(value = "批量或单个删除系统管理员")
    @DeleteMapping("/{userIds}")
    @PreAuthorize("hasAnyAuthority('sys:user:delete')")
    public Result removeSysUser(@PathVariable List<Long> userIds){
        Boolean removed = sysUserService.removeSysUserByUserIds(userIds);
        return Result.handle(removed);
    }

}
